using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using XYThreadPoolLib;
using System.Diagnostics;
using Magnolia;

namespace CommLayer
{
    class InputProcessorMedialonClient : InputProcessorMedialon
    {
        protected delegate void LoopProcessInputDelegate(Transmission trn, IPEndPoint ipe);
        protected TcpIpClientMedialon m_parent;
        bool m_bProcessInput = true;
        /// <summary>
        /// Constrctor medialon client input processor
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="suffix"></param>
        /// <param name="delegateConnectionFilter"></param>
        /// <param name="delegateStringInputHandler"></param>
        /// <param name="delegateExceptionHandler"></param>
        public InputProcessorMedialonClient(byte prefix, byte suffix, StringInputHandlerDelegate delegateStringInputHandler,TcpIpClientMedialon parent)
            :
                base(prefix, suffix, delegateStringInputHandler, parent.ThreadPool)
        {
            Parent = parent;
        }

        /// <summary>
        /// Parent container of the input processor
        /// </summary>
        public TcpIpClientMedialon Parent
        {
            get
            {
                return m_parent;
            }
            set
            {
                m_parent = value;
            }
        }


        /// <summary>
        /// client starts the main thread in a loop
        /// </summary>
        /// <param name="trn"></param>
        /// <param name="ipe"></param>
        public override void Start(Transmission trn, IPEndPoint ipe)
        {
            // start the loop 
            m_bProcessInput = true;
            // and the input processin thread 
            m_threadPool.InsertWorkItem("Process Input", new LoopProcessInputDelegate(LoopProcessInput), new Object[2] { trn, ipe }, false);
        }

        /// <summary>
        /// Loop over process input
        /// </summary>
        /// <param name="trn"></param>
        /// <param name="ipe"></param>
        public virtual void LoopProcessInput(Transmission trn, IPEndPoint ipe)
        {
            while (m_bProcessInput)
            {
                ProcessInput(trn, ipe);
            }
        }

        /// <summary>
        /// Handle client exception
        /// </summary>
        public override void HandleException(Exception oBug, Transmission trn)
        {
            // trace from here too
            Trace.WriteLineIf(AppContext.AppTraceSwitch.TraceError, DateTime.Now.ToLongTimeString() + "-> InputProcessorMedialonClient.HandleException: " + oBug.Message);
            // stop the input processing 
            m_bProcessInput = false;
            // and process the exception, which will close the connection
            Parent.HandleException(oBug,trn);
        }
    }
}
